<?php
/**
 * 去年的今天钩子
 * @author 吕秀才
 * @version TS3.0
 */
class ChangeLoginBgHooks extends Hooks
{
	/**
     * 钩子
     * @return void
     */
    public function ChangeLoginBg()
    {
       	$this->get_bg();
       	$s = model('Xdata')->get('ChangeLoginBgM');
       	$s = $s ? $s : 3000;
       	$this->assign('s',$s);
       	$this->assign('js_css_path',$this->htmlPath);        
       	$this->display('changebg_front');
    }
	/**
	 * 获取数据
	 */
    private function get_bg(){    	
    	$imglist = model('Xdata')->get('ChangeLoginBg');
    	
    	if(empty($imglist)){			
			echo  APP_PUBLIC_URL . '/image/body-bg2.jpg';//默认背景图片			
		}
		else{
			//$ids = explode(',', $imglist);
			$map['attach_id'] =	array('IN', $imglist);
			$map['is_del'] = 0;			
			if($attachInfo = M('Attach')->where($map)->field('*')->findAll()){				
				$strList = array();				
				foreach($attachInfo as $v){
					$strList[] = "'" .getImageUrl($v['save_path'].$v['save_name']) . "'";
				}				
				$data = implode(',',$strList);
				$this->assign('data',$data);				
			}else{
				echo  APP_PUBLIC_URL . '/image/body-bg2.jpg';
			}			
		}    	
    	
    }
    /**
     * 后台配置管理
     */
    public function config(){    	
    	$this->display('changebg_admin');
    }
    /**
     * 保存配置
     */
   public function saveconfig(){
		if(!empty($_POST) && isset($_POST['attach_ids'])){//post请求且ids存在
			$id = intval(trim($_POST['attach_ids'],'|'));    		
			if($id > 0){
				$ids = model('Xdata')->get('ChangeLoginBg');//获取原数据
				if(empty($ids)){
					$ids = array();
				}
				if(count($ids) < 7){ //限制最多为7张图片
					array_push($ids,$id);//合并之前的数据
					model('Xdata')->put('ChangeLoginBg',$ids);//存入缓存
				}
				else{
					die("最多为7张图片");
				}				
			}   		
		}   	
   }
   
   public function manage(){		
		$ids = model('Xdata')->get('ChangeLoginBg');
		//!is_array($ids) && $ids = explode(',', $ids);
		$map['attach_id'] =	array('IN', $ids);
		$map['is_del'] = 0;
		$listData = M('Attach')->where($map)->field('*')->findAll();
		//var_dump($listData);die;
		$image = array('png','jpg','gif','jpeg','bmp');
		foreach($listData as &$v){
			$user = model('User')->getUserInfo($v['uid']);
			$v['uname'] = $user['uname'];
			$v['size'] = byte_format($v['size']);
			$v['name']	   = in_array($v['extension'],$image) 
							? '<a href="'.U('widget/Upload/down',array('attach_id'=>$v['attach_id'])).'">'.
								"<img src='".UPLOAD_URL."/".$v['save_path'].$v['save_name']."' width='100'><br/>{$v['name']}</a>"
							:'<a href="'.U('widget/Upload/down',array('attach_id'=>$v['attach_id'])).'">'.$v['name'].'</a>';
		}
		$this->assign('listData',$listData);
		$this->display('manage');		
   }
   
   public function dodel(){
		$result = array();
		$ids = t($_POST['ids']);
		$ids = explode(',',$ids);
		
		if(empty($ids)) {
			$result['status'] = 0;
			$result['info'] = '参数不能为空';
			exit(json_encode($result));
		}
		foreach($ids as $id){
			if($id != 0){
				model('Attach')->doEditAttach($id,'delAttach',L('PUBLIC_STREAM_DELETE'));
				$img_ids = model('Xdata')->get('ChangeLoginBg');//获取原数据
				$key = array_search($id);//查找id所在数组中的位置
				unset($img_ids[$key]);//删除
				model('Xdata')->put('ChangeLoginBg',$img_ids);//重新保存到缓存里
			}			
		}		
		
		$result['status'] = 1;
		$result['info'] = '删除成功';		
		exit(json_encode($result));
   }
   public function param(){
   		$s = model('Xdata')->get('ChangeLoginBgM');
   		$s = $s ? $s : 3000;
   		$this->assign('s',$s);   		
   		$this->display('param_admin');
   }
   public function doparam(){//设置图片的切换时间
   		if(!empty($_POST) && isset($_POST['param'])){//post请求且param存在
	   		$s = intval($_POST['param']);
	   		model('Xdata')->put('ChangeLoginBgM',$s);//存入缓存
   		}
   }
}